package com.catt.orderdinner.service;

import com.catt.orderdinner.common.KeyUtils;
import com.catt.orderdinner.common.dateUtils;
import com.catt.orderdinner.dataobject.*;
import com.catt.orderdinner.form.OrderForm;
import com.catt.orderdinner.repository.OrderDetailRepository;
import com.catt.orderdinner.repository.TbFoodRepository;
import com.catt.orderdinner.repository.TbOrderRepository;
import com.catt.orderdinner.repository.TbUserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class OrderService {

    @Autowired
    private OrderDetailRepository orderDetailRepository;
    @Autowired
    private TbFoodRepository tbFoodRepository;
    @Autowired
    private TbOrderRepository tbOrderRepository;
    @Autowired
    private TbUserRepository tbUserRepository;

    public void commitOrder(String userId,String[] foodIds,String amount){
        //存order表
        int amounti = Integer.parseInt(amount);
        TbUser one = tbUserRepository.getOne(userId);
        TbOrder tbOrder = new TbOrder();
        String orderId = KeyUtils.genUniqueKey();
        tbOrder.setOrderId(orderId);
        tbOrder.setUserId(userId);
        tbOrder.setUserName(one.getUserName());
        tbOrder.setOrderAmount(amounti);
        tbOrder.setCreateTime(new Date());
        tbOrderRepository.save(tbOrder);
        //然后存 orderdetail表
        for (String foodId : foodIds){
            TbFood food = tbFoodRepository.getOne(foodId);
            String detailId = KeyUtils.genUniqueKey();
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setDetailId(detailId);
            orderDetail.setOrderId(orderId);
            orderDetail.setFoodId(foodId);
            orderDetail.setFoodName(food.getFoodName());
            orderDetail.setFoodPrice(food.getPrice());
            orderDetail.setImgFood(food.getImgFood());
            orderDetailRepository.save(orderDetail);
        }

    }

    public List<OrderForm> list(String userId){
        List<TbOrder> orders = tbOrderRepository.findByUserId(userId);
        List<OrderForm> result = new LinkedList<>();
        for (TbOrder order : orders){
            //拿到orderId 然后去查detail
            String datestr = "SO COOL";
            if (order.getCreateTime()!=null){
                datestr = dateUtils.formatTime(order.getCreateTime());
            }
            OrderForm orderForm = new OrderForm();
            List<OrderDetail> orderdetails = orderDetailRepository.findByOrderId(order.getOrderId());
            orderForm.setOrder(order);
            orderForm.setOrderDetails(orderdetails);
            orderForm.setOrderTime(datestr);
            result.add(orderForm);
        }
        return result;
    }

    public Page<List<Map>> getNowOrder(Date star, Date end, Pageable pageable){
        return orderDetailRepository.getNowOrder(star,end,pageable);
    }

    public Page<List<Map>> getMonthlyOrder(String monthly, Pageable pageable){
        return tbOrderRepository.getMonthlyOrder(monthly,pageable);
    }

    public List<Map> getMonthlyOrder(String monthly){
        return tbOrderRepository.getMonthlyOrder(monthly);
    }
}
